<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	.img-outer {
		display: flex;
		flex-wrap: wrap;
	}
	#dynamic-table img {
		cursor: pointer;
		margin: 10px;
	}
	#cboxClose {
		text-indent: 0;
		font-size: 50px;
		font-weight: bold;
		margin-top: 10px;
		color: red;
	}
	.div-date-picker {
		position: relative;
	}
	#date-picker {
		width: 168px;
		padding-left: 30px;
	}
	.div-date-picker .fa-calendar{
		position: absolute;
		left: 8px;
		top: 9px;
	}
</style>
<div class="page-header">
	<h1>
		德育评价管理
		<?php if($this->userType == USER_TYPE_TEACHER): ?>
			<small>
				<a href="<?=admin_url('moral/remark_edit')?>" class="blue ml10">
					<i class="ace-icon fa fa-plus"></i> 新增评价
				</a>
			</small>
		<?php endif ?>
	</h1>
</div>
<div class="col-xs-12 content-filter">
	<div class="widget-box">
		<div class="widget-header" style="vertical-align:middle;">
			<h4 class="widget-title">
				内容筛选
			</h4>
			<div class="widget-toolbar">
				<a href="#" data-action="collapse">
					<i class="ace-icon fa fa-chevron-up"></i>
				</a>
				<a href="#" data-action="close">
					<i class="ace-icon fa fa-times"></i>
				</a>
			</div>
		</div>
		<div class="widget-body">
			<div class="widget-main">
				<div class="form-inline keyword-search-div">
					<label class="keyword-search-label">名称&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
					<div class="keyword-search-box">	
                        <input style="float:left;height:30px;width:220px;border:1px solid #669FC7;padding:0 30px 0 14px;border-radius:10px 0 0 10px !important;-moz-border-radius:10px 0 0 10px;" type="text" name="realname" placeholder="搜索学生名称..." autocomplete="off">
                        <span id="empty-search" style="position:relative;right:64px;cursor:pointer;display:none;">×</span>
                        <button id="btn-search" class="btn-search" style="outline:none;float:left;height:30px;width:44px;border:1px solid #669FC7;background-color:#669FC7;border:0;border-radius:0 10px 10px 0;-moz-border-radius:0 10px 10px 0;">
                            <i class="ace-icon fa fa-search nav-search-icon white"></i>
                        </button>
					</div>
				</div>
				<!-- 管理员权限 -->
				<?php if($rmxUser['user_type'] == USER_TYPE_SCHOOL): ?>
					<div class="form-inline keyword-search-div" id="div-grade">
						<label class="keyword-search-label">年级&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
						<div class="keyword-search-box">
							<a href="javascript:;" class="keyword-search-keyword keyword-search-grade active" data-id="">全部</a>
							<?php if(isset($gradeList)): ?>
								<?php foreach($gradeList as $grade): ?>
									<a href="javascript:;" class="keyword-search-keyword keyword-search-grade"  data-id="<?=$grade['id']?>"><?=$grade['grade_name']?></a>
								<?php endforeach; ?>
							<?php endif ?>
						</div>
					</div>
					<div class="form-inline keyword-search-div" id="div-normal-class">
						<label class="keyword-search-label">班级&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
						<div class="keyword-search-box">
							<a href="javascript:;" class="keyword-search-keyword keyword-search-normal-class active" data-id="">全部</a>
						</div>
					</div>
				<?php endif ?>
                <div class="form-inline keyword-search-div">
					<label class="keyword-search-label">结果&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
					<div class="keyword-search-box" id="div-result">
						<a href="javascript:;" class="keyword-search-keyword active" data-id="0">全部</a>
						<?php foreach($resultList as $result): ?>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=$result['id']?>"><?= $result['title'] ?></a>
						<?php endforeach; ?>
					</div>
				</div>
				<div class="form-inline keyword-search-div" id="div-createtime">
					<label class="keyword-search-label">创建日期</label>
					<div class="keyword-search-box">
						<div class="div-date-picker">
							<input class="form-control" type="text" id="date-picker" name="createdate" value=""/>
							<i class="fa fa-calendar"></i>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>学生名称</th>
				<th>就读班级</th>
				<th>评价结果</th>
				<th>评价内容</th>
                <th width="40%">证明材料</th>
				<th>创建时间</th>
				<?php if($this->userType == USER_TYPE_TEACHER): ?>
					<th>操作</th>
				<?php endif ?>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){
		// 班级数据
		var normalClassJson = <?= $gradeNormalClassList ?>;
		// 评价表格
		var table = $('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('moral/get_remark_list')?>',
			"columns": [
                {"data": "realname"},
                {"data": "class_name"},
				{"data": "resultFormat"},
				{"data": "content"},
	            {"data": function(ret){
					var imgArr = ret.imgArr;
					var str = '<div class="img-outer">';
					for(let i in imgArr) {
						var img = imgArr[i];
						str += '<div class="ace-thumbnails"><a href="' + img + '" data-rel="colorbox" class="cboxElement"><img src="' + img + '!thumb" width="128"/></a></div>';
					}
					str += '</div>'
					return str;
					// if(ret.material) {
					// 	return '<div class="ace-thumbnails"><a href="' + ret.material + '" data-rel="colorbox" class="cboxElement"><img src="' + ret.material + '" width="128"/></a></div>';
					// } else {
					// 	return '';
					// }
	            }},
	            {"data": "createtime"},
				<?php if($this->userType == USER_TYPE_TEACHER): ?>
					{"data": function(ret){
						var action = '';
						action += '<a class="edit-remark green mr20" href="<?= admin_url('moral/remark_edit/') ?>' + ret.id + '"><i class="fa fa-edit"></i> 修改</a>';
						action += '<a class="text-danger delete-remark" href="javascript:" data-id=' + ret.id +  ' data-title=' + ret.realname + '><i class="fa fa-times"></i> 删除</a>';
						return action;
					}}
				<?php endif ?>
			]
		});
		// 内容筛选
		$("#data-search-btn").click(function(){
			search(table);
		})
		// 年级班级联动
		$("#data-search-grade").change(function(){
			var gradeId = $(this).val();
			var classSelect = $("#data-search-class");
			var getUrl = "<?=admin_url('school/get_class_list')?>/" + gradeId;
			ajax_get(getUrl, {hideLoading: true}, function(res){
				var optionHtml = "";
				classSelect.html('<option value="">全部班级</option>');
				for(var i in res.data) {
					var classData = res.data[i];
					if(classData.class_type == '<?=CLASSES_TYPE_NORMAL?>') classSelect.append('<option value="'+ classData.id +'">'+ classData.class_name +'</option>');
				}
			})
		})
		// 点击关键字
		$("body").on("click", ".keyword-search-keyword", function(){
			var id = $(this).data("id");
			// 高亮当前点击对象
			$(this).parent().find(".keyword-search-keyword").removeClass("active");
			$(this).addClass("active");
			// 如果是点击年级
			if($(this).hasClass("keyword-search-grade")) {
				// 先清空班级列表
				$(".keyword-search-normal-class:gt(0)").remove();
				// 获取班级列表
				var normalClassList = normalClassJson[id];
				for(var i in normalClassList) $(".keyword-search-normal-class:first").parent().append('<a href="javascript:;" class="keyword-search-keyword keyword-search-normal-class" data-id="'+ normalClassList[i].id +'">'+ normalClassList[i].name +'</a>');
				// 选中全部班级
				var realname = $("[name=realname]").val();
				var gradeId = $("#div-grade .active").data("id");
				search(table);
			} else {
				search(table);
			}
		});
		// 敲回车就搜索
		$("[name=realname]").keypress(function (e) {
                if (e.which == 13) {
                    $("#btn-search").trigger("click");
                }
		});
		// 清空搜索框
		$("[name=realname]").keyup(function() {
			if($(this).val().length > 0) {
				$("#empty-search").show();
				$("#empty-search").click(function(){
					$("[name=realname]").val('');
					$(this).hide();
					// 重置搜索结果
					search(table);
				})
			} else {
				$("#empty-search").hide();
			}
		}); 
		// 搜索标题
		$("#btn-search").click(function() {
			search(table);
		});

		$("#work-calendar").click(function() {
			if($(this).text() == '全部') {
				//执行一个laydate实例
				var chooseDate = getNowDate();
				var today = getNowDate();
				//设置作业时间区间开始为前6天
				var sixDayBefore = getBeforeSixDate();
				var rangeDate = sixDayBefore + ' - ' + today;
				laydate.render({
					elem: '#work-calendar', //指定元素
					value: rangeDate,
					range: true,
					max: today,
					btns: ['confirm'],
					done: function(value) {
						search(table, value);
					}
				});
				search(table);
			}
		});
		// 监听创建时间变化
		$("[name=createdate]").blur(function() {
			search(table);
		});
		$('#date-picker').datetimepicker({
			format: 'YYYY-MM-DD',
			maxDate: "<?= date('Y-m-d') ?>",
		});
		// 点击日期图标触发点击日期input
		$(".fa-calendar").click(function() {
			$('#date-picker').focus();
		});
		// 保存评价按钮
		var remarkForm = $("#remark-form");
		$("#save-remark").click(function(){
			remarkForm.submit();
		})
		var submitUrl = "<?=admin_url('moral/save_remark_action')?>";
		form_submit(remarkForm, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})
		// 删除评价
		$("body").on('click', '.delete-remark', function() {
			var id = $(this).data('id');
			var title = $(this).data('title');
			var posturl = "<?= admin_url("moral/delete_remark_action") ?>";
			var data = {
				'id': id
			};
			layer.confirm('确定删除：' + title + '的德育评价？', { title:"提示", offset: '160px' }, function(){
				ajax_post(posturl, data, function() {
					// 刷新页面
					window.location.reload();
				});
			});
		});
		// 图片轮播
		var colorbox_params = {
			rel: 'colorbox',
			reposition:true,
			scalePhotos:true,
			scrolling:false,
			previous:'<i class="ace-icon fa fa-arrow-left"></i>',
			next:'<i class="ace-icon fa fa-arrow-right"></i>',
			close:'&times;',
			current:'',
			maxWidth:'100%',
			maxHeight:'100%',
			onOpen:function(){
				$overflow = document.body.style.overflow;
				document.body.style.overflow = 'hidden';
			},
			onClosed:function(){
				document.body.style.overflow = $overflow;
			},
			onComplete:function(){
				$.colorbox.resize();
			}
		};
		$('.ace-thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);
		setTimeout(() => {
			$('.ace-thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);
		}, 100);
		setTimeout(() => {
			$('.ace-thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);
		}, 1000);
		setTimeout(() => {
			$('.ace-thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);
		}, 2000);
		setTimeout(() => {
			$('.ace-thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);
		}, 3000);
		setTimeout(() => {
			$('.ace-thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);
		}, 5000);
	})
	function search(table, timeStr='') {
		if(timeStr == '') {
			timeStr = $("#work-calendar").text();
		}
		var timeArr = [];
		var beginTime = '';
		var endTime = '';
		if(timeStr.indexOf(" - ") != -1) {
			timeArr = timeStr.split(" - ");
			beginTime = timeArr[0] + ' 00:00:00';
			endTime = timeArr[1] + ' 23:59:59';
		}

		table.setAjaxData({
			"grade_id": $("#div-grade .active").data('id'),
			"class_id": $("#div-normal-class .active").data('id'),
			"realname": $("[name=realname]").val(),
			"result": $("#div-result .active").data('id'),
			'createdate': $("[name=createdate]").val()
		});
	}
</script>